//创建窗口

import { BrowserWindow } from 'electron'
import { join } from 'path'
import { is } from '@electron-toolkit/utils'
import global from './global'
import win32Utils from './win32Utils'
import globalPublic from './public'

export default class initWindow {
  static createFollowWindow(platform: string): void {
    global.followWindow = new BrowserWindow({
      width: 300,
      height: 800,
      show: false,
      frame: false,
      resizable: false,
      webPreferences: {
        preload: join(__dirname, '../preload/index.js'),
        sandbox: false
      }
    })

    global.followWindow.on('ready-to-show', async () => {
      global.followWindow?.webContents.send('current-platform', platform)
      // 等待窗口初始化后调用跟随
      let pid: number = (await globalPublic.getPidByName('PddWorkbench.exe')) as number
      win32Utils.pollFollowWindow(global.followWindow as BrowserWindow, '拼多多工作台', pid)
    })

    const rendererUrl =
      is.dev && process.env['ELECTRON_RENDERER_URL']
        ? process.env.ELECTRON_RENDERER_URL
        : `file://${join(__dirname, '../renderer/index.html')}`

    global.followWindow.loadURL(`${rendererUrl}#/follow-window`)
  }
}
